package com.platform.api;

import com.platform.annotation.IgnoreAuth;
import com.platform.annotation.LoginUser;
import com.platform.entity.*;
import com.platform.service.*;
import com.platform.util.ApiBaseAction;
import com.platform.utils.Query;
import com.platform.utils.R;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者: @author zhurusen <br>
 * 时间: 2017-08-11 08:32<br>
 * 描述: ApiIndexController <br>
 */
@RestController
@RequestMapping("/api/index")
public class ApiIndexController extends ApiBaseAction {

    private static final Log log = LogFactory.getLog(ApiIndexController.class);


    @Autowired
    private ApiUserService apiUserService;
    @Autowired
    private ApiUserVideoService apiUserVideoService;
    @Autowired
    private ApiUserVideoBillService apiUserVideoBillService;
    @Autowired
    private ApiCallUserBillService callUserBillService;


    /**
     * 精选、新人列表
     *
     * @param type 类型 1 精选 2 新人
     * @param page
     * @return
     */
    @RequestMapping("zbList")
    public R zbList(@LoginUser UserVo loginUser, Integer type, String nickName, @RequestParam(value = "page", defaultValue = "1") Integer page) {

        Map<String, Object> params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);
        params.put("identity", 2);
        if (type != null) {
            params.put("type", type);
        }
        params.put("nickname", nickName);
//        if (loginUser != null) {
//            params.put("zbUserid", loginUser.getId());
//        }
        params.put("sidx", "sort");
        params.put("order", "desc");
        Query query = new Query(params);

        List<UserVo> list = apiUserService.queryList(query);

        List<UserInfo> resultList = new ArrayList<>();
        for (UserVo info : list) {
            UserInfo userInfo = new UserInfo();
            userInfo.setId(info.getId());
            userInfo.setNickName(info.getNickname());
            userInfo.setOnlineStatus(info.getOnline_status());
            userInfo.setPoster(info.getCover_url());
            userInfo.setIntroduction(info.getIntroduction());
            userInfo.setLivePrice(info.getLive_price());
            userInfo.setVoiceUrl(info.getVoice_url());
            resultList.add(userInfo);
        }

//        /**
//         * 统计通话记录
//         */
//        if (page == 1) {
//            callUserBillService.updateCallList(loginUser);
//        }

        return R.ok(0, "获取成功", resultList);
    }

    /**
     * 热秀和我的关注
     *
     * @param loginUser
     * @param type      1 热秀 2 我的关注
     * @param page
     * @return
     */
    @RequestMapping("zbHotShowist")
    public R zbHotShowist(@LoginUser UserVo loginUser, Integer type, @RequestParam(value = "page", defaultValue = "1") Integer page) {
        Map<String, Object> params = new HashMap();
        params.put("page", page);
        params.put("limit", 10);
        if (loginUser != null && type != null && type == 2) {
            params.put("cmUserId", loginUser.getId());
        } else {
            params.put("payStatus", 2);
        }

        params.put("sidx", "create_date");
        params.put("order", "desc");
        Query query = new Query(params);

        List<ApiUserVideoVo> videoVos = apiUserVideoService.selectList(query);
        for (ApiUserVideoVo info : videoVos) {
            UserInfo userInfo = apiUserService.toUserInfo(info.getUserId(), loginUser);

            UserInfo vo = new UserInfo();
            vo.setId(userInfo.getId());
            vo.setAvatar(userInfo.getAvatar());
            vo.setNickName(userInfo.getNickName());
            vo.setMobile(userInfo.getMobile());
            vo.setLivePrice(userInfo.getLivePrice());

            info.setUserVo(vo);

            Map<String, Object> param_bill = new HashMap();
            param_bill.put("userVideoId", info.getId());
            if (loginUser != null) {
                param_bill.put("userId", loginUser.getId());
            }

            List<ApiUserVideoBillVo> list = apiUserVideoBillService.selectList(param_bill);

            if (list.size() > 0) {
                ApiUserVideoBillVo apiUserVideoBillVo = list.get(0);
                if (apiUserVideoBillVo.getLike() == 1) {
                    info.setIsLike(1);
                }
                if (apiUserVideoBillVo.getPayStatus() == 1) {
                    info.setPayStatus(2);
                }
            }
        }
        return R.ok(0, "获取成功", videoVos);
    }


    /**
     * @param type 1分成计划 2探索发布规范  3特殊情况须知4 用户行为规范
     * @return
     */
    @IgnoreAuth
    @RequestMapping("res")
    public R res(Integer type) {
        String url = "";
        if (type == 1) {
            url = "https://www.qutuiya.com/h/img/fencheng.png";
        }
        if (type == 2) {
            url = "https://www.qutuiya.com/h/img/tansuo_help.png";
        }
        if (type == 3) {
            url = "https://www.qutuiya.com/h/img/help.png";
        }
        if (type == 4) {

            url = "https://www.qutuiya.com/h/img/xw_help.png";
        }

        return R.ok(0, "获取成功", url);
    }

}